﻿using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Estsh.Core.Model.EnumUtil.WmsEnumUtil;

namespace Estsh.Core.Wms.Services
{
	public class NCMoveService : BaseService<BaseEntity>, INCMoveService
	{
		private readonly INCMoveRepository repository;
		public NCMoveService(INCMoveRepository _repository) : base(_repository)
		{
			repository = _repository;
		}

		/// <summary>
		/// /判断库位
		/// </summary>
		/// <param name="loginId"></param>
		/// <param name="LocateName"></param>
		/// <param name="isPack"></param>
		/// <returns></returns>
		public string CheckLocateName(string loginId, string LocateName, string isPack)
		{
			List<SysLocate> sysLocates = repository.CheckLocateStatus(LocateName);
			if (sysLocates.Count == 0)
			{
				return "[" + LocateName + "]:库位不存在，请检查!";
			}

			List<SysStock> sysStocks = repository.CheckUseStockCapacity(LocateName);
			if (sysStocks.Count == 0)
			{
				return "OK";
			}
			if (sysStocks[0].LocateNum > sysLocates[0].LocateCapacity)
			{
				return "[" + LocateName + "]:库位容量已满，请检查！";
			}


			return "OK";
		}

		/// <summary>
		/// 执行NC移库
		/// </summary>
		/// <param name="user">用户名</param>
		/// <returns></returns>
		public string CheckNCMove(string loginId, string cartonNo, string locateName, string isPack)
		{
			//判断箱条码是否可用
			SysStock cartonInfo = repository.CheckCartonNoStatus(cartonNo);

			if (cartonInfo == null)
			{
				return "[" + cartonNo + "]:条码不可用，请检查!";
			}
			if (cartonInfo.Enabled.Equals("N"))
			{
				return "[" + cartonNo + "]:箱条码已冻结，无法操作!";
			}
			if (cartonInfo.Status != (int)StockStatus.NC_INSTOCK)
			{
				return "[" + cartonNo + "]:该条码为" + cartonInfo.StockStatus + "状态，请检查!";
			}
			if (cartonInfo.CartonType != 0)//条码类型   0:箱条码、1：周转箱 2：托条码
			{
				return "[" + cartonNo + "]:该条码不为箱条，请检查！";
			}


			//获取库位表库位与绑定零件信息
			List<SysLocate> sysLocates = repository.CheckLocateStatus(locateName);
			List<SysStock> sysLocatesSum = repository.CheckUseStockCapacity(locateName);
			decimal QtySum = sysLocatesSum.Sum(a => a.Qty);//已经使用库存总量
			if (sysLocates[0].LocateType == (int)WmsEnumUtil.LocateType.NC_LOCATE)
			{

				if (sysLocates[0].LocateCapacity > QtySum + cartonInfo.Qty)
				{
					//动态组合库库位正确 ，有库容，正常入库
					List<SysStock> sysStocks = new List<SysStock>();
					sysStocks.Add(cartonInfo);
					return repository.UpdateStockStatus(sysStocks, sysLocates, (int)WmsEnumUtil.StockStatus.NC_INSTOCK, (int)WmsEnumUtil.TransType.MOVESTOCK, loginId);

				}
				else
				{
					return "[" + locateName + "]:库位容量不足，请检查！";
				}

			}
			else
			{
				return "[" + locateName + "]:库位类型异常，请检查！";
			}
				




		}


		/// <summary>
		/// 获取信息列表
		/// </summary>
		/// <param name="cartonNo"></param>
		/// <param name="isPack"></param>
		/// <returns></returns>
		public List<SysStock> GetNCMovePartInfo(string cartonNo, string isPack)
		{
			//单包装移
			return repository.GetCartonNoInfo(cartonNo);
		}


	}
}
